package cn.coderdream.widget.imageview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.*;
import android.util.AttributeSet;
import androidx.annotation.ColorInt;
import androidx.annotation.Dimension;
import androidx.appcompat.widget.AppCompatImageView;
import cn.coderdream.widget.R;

/**
 * 作者: mengpeng
 * 时间: 2019-05-13 - 14:20
 * 用途: 各种角的imageview
 */
public class MImageView extends AppCompatImageView {
    private Context mContext;
    @Dimension
    private float mImageView_corners = 0f;
    @Dimension
    private float mImageView_leftTop_corners = 0f;
    @Dimension
    private float mImageView_leftBottom_corners = 0f;
    @Dimension
    private float mImageView_rightTop_corners = 0f;
    @Dimension
    private float mImageView_rightBottom_corners = 0f;
    @Dimension
    private float mImageView_stroke_width = 0f;
    @ColorInt
    private int mImageView_stroke_color;

    private float[] radiusArray = {0f, 0f, 0f, 0f, 0f, 0f, 0f, 0f};

    private Paint mPaint;

    public MImageView(Context context) {
        this(context, null);
    }

    public MImageView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initPaint();
        init(attrs, defStyleAttr);
    }

    private void init(AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = mContext.getTheme().obtainStyledAttributes(attrs, R.styleable.MImageView, defStyleAttr, 0);
        try {
            int indexCount = typedArray.getIndexCount();
            for (int i = 0; i < indexCount; i++) {
                int index = typedArray.getIndex(i);
                if (index == R.styleable.MImageView_mImageView_corners) {
                    mImageView_corners = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_leftTop_corners) {
                    mImageView_leftTop_corners = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_leftBottom_corners) {
                    mImageView_leftBottom_corners = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_rightTop_corners) {
                    mImageView_rightTop_corners = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_rightBottom_corners) {
                    mImageView_rightBottom_corners = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_stroke_width) {
                    mImageView_stroke_width = typedArray.getDimension(index, 0);
                } else if (index == R.styleable.MImageView_mImageView_stroke_color) {
                    mImageView_stroke_color = typedArray.getColor(index, -1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            typedArray.recycle();
        }
    }

    private void initPaint() {
        mPaint = new Paint();
        //设置填充样式
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置边界模糊
        mPaint.setDither(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        //1.处理一下四个角
        initCorners();
        //2.设置画笔和画布都是抗锯齿
        initCanvas(canvas);
        //3.裁剪画布
        initClipCanvas(canvas);
        //4.绘制图片
        initDraw(canvas);
        super.onDraw(canvas);
        //5.绘制边框
        drawStroke(canvas);
    }

    /**
     * 1.处理一下四个角
     */
    private void initCorners() {
        if (mImageView_corners > 0) {
            mImageView_leftTop_corners = mImageView_corners;
            mImageView_leftBottom_corners = mImageView_corners;
            mImageView_rightTop_corners = mImageView_corners;
            mImageView_rightBottom_corners = mImageView_corners;
        }
        radiusArray[0] = mImageView_leftTop_corners;
        radiusArray[1] = mImageView_leftTop_corners;
        radiusArray[2] = mImageView_rightTop_corners;
        radiusArray[3] = mImageView_rightTop_corners;
        radiusArray[4] = mImageView_rightBottom_corners;
        radiusArray[5] = mImageView_rightBottom_corners;
        radiusArray[6] = mImageView_leftBottom_corners;
        radiusArray[7] = mImageView_leftBottom_corners;
    }

    /**
     * 2.设置画笔和画布都是抗锯齿
     */
    private void initCanvas(Canvas canvas) {
        // 画布绘制Bitmap时搞锯齿
        canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
        // 设置转换模式（显示Scr与Dst交接的区域）
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }

    /**
     * 3.裁剪画布
     */
    private void initClipCanvas(Canvas canvas) {
        RectF rectF = new RectF();
        rectF.left = 0;
        rectF.top = 0;
        rectF.right = getWidth();
        rectF.bottom = getHeight();
        Path path = new Path();
        path.addRoundRect(rectF, radiusArray, Path.Direction.CW);
        //裁剪
        canvas.clipPath(path);
    }

    /**
     * 4.绘制图片
     */
    private void initDraw(Canvas canvas) {
        RectF rectF1 = new RectF();
        rectF1.left = mImageView_stroke_width / 2;
        rectF1.top = mImageView_stroke_width / 2;
        rectF1.right = getWidth() - mImageView_stroke_width / 2;
        rectF1.bottom = getHeight() - mImageView_stroke_width / 2;
        Path path1 = new Path();
        path1.addRoundRect(rectF1, radiusArray, Path.Direction.CW);
        // 绘制Dst
        canvas.drawPath(path1, mPaint);
    }

    /**
     * 5.绘制边框
     */
    private void drawStroke(Canvas canvas) {
        canvas.save();
        canvas.translate(0.0F, 0.0F);
        mPaint.reset();
        mPaint.setColor(mImageView_stroke_color);
        mPaint.setStrokeWidth(mImageView_stroke_width);
        mPaint.setStyle(Paint.Style.STROKE);
        //设置抗锯齿
        mPaint.setAntiAlias(true);
        //设置边界模糊
        mPaint.setDither(true);
        RectF rectF = new RectF();
        rectF.left = mImageView_stroke_width / 2;
        rectF.top = mImageView_stroke_width / 2;
        rectF.right = getWidth() - mImageView_stroke_width / 2;
        rectF.bottom = getHeight() - mImageView_stroke_width / 2;
        Path path = new Path();
        path.addRoundRect(rectF, radiusArray, Path.Direction.CW);
        canvas.drawPath(path, mPaint);
    }

}
